package com.push.web.controller;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.push.common.annotation.Log;
import com.push.common.core.controller.BaseController;
import com.push.common.core.domain.AjaxResult;
import com.push.common.enums.BusinessType;
import com.push.web.domain.BizLabelSecond;
import com.push.web.service.IBizLabelSecondService;
import com.push.common.utils.poi.ExcelUtil;
import com.push.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 二级分类Controller
 *
 * @author yanxi
 * @date 2022-03-09
 */
@RestController
@RequestMapping("/information/labelSecond")
public class BizLabelSecondController extends BaseController
{
    @Autowired
    private IBizLabelSecondService bizLabelSecondService;

    /**
     * 查询二级分类列表
     */
    @PreAuthorize("@ss.hasPermi('information:label:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizLabelSecond bizLabelSecond)
    {
        startPage();
        List<BizLabelSecond> list = bizLabelSecondService.selectBizLabelSecondList(bizLabelSecond);
        return getDataTable(list);
    }

    /**
     * 导出二级分类列表
     */
    @PreAuthorize("@ss.hasPermi('information:label:export')")
    @Log(title = "二级分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizLabelSecond bizLabelSecond)
    {
        List<BizLabelSecond> list = bizLabelSecondService.selectBizLabelSecondList(bizLabelSecond);
        ExcelUtil<BizLabelSecond> util = new ExcelUtil<BizLabelSecond>(BizLabelSecond.class);
        return util.exportExcel(list, "二级分类数据");
    }

    /**
     * 获取二级分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('information:label:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizLabelSecondService.selectBizLabelSecondById(id));
    }

    /**
     * 新增二级分类
     */
    @PreAuthorize("@ss.hasPermi('information:label:add')")
    @Log(title = "二级分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizLabelSecond bizLabelSecond)
    {
        return toAjax(bizLabelSecondService.insertBizLabelSecond(bizLabelSecond));
    }

    /**
     * 修改二级分类
     */
    @PreAuthorize("@ss.hasPermi('information:label:edit')")
    @Log(title = "二级分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizLabelSecond bizLabelSecond)
    {
        return toAjax(bizLabelSecondService.updateBizLabelSecond(bizLabelSecond));
    }

    /**
     * 删除二级分类
     */
    @PreAuthorize("@ss.hasPermi('information:label:remove')")
    @Log(title = "二级分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizLabelSecondService.deleteBizLabelSecondByIds(ids));
    }

    /**
     * 批量上传
     */
    @RequestMapping("/importData")
    @ResponseBody
    public AjaxResult importData(@RequestParam MultipartFile file, @RequestParam boolean updateSupport, @RequestParam long parentId) throws Exception {
        ExcelUtil<BizLabelSecond> util = new ExcelUtil<BizLabelSecond>(BizLabelSecond.class);
        List<BizLabelSecond> labels = util.importExcel(file.getInputStream());
        //循环插入数据
        for (BizLabelSecond label:labels) {
            label.setParentId(parentId);
            List<BizLabelSecond> result = bizLabelSecondService.selectBizLabelSecondList(label);
            if(result.size() > 0) {
                //如果设置了更新
                if(updateSupport) {
                    bizLabelSecondService.updateBizLabelSecond(label);
                }
            } else {
                //之前不存在直接插入
                bizLabelSecondService.insertBizLabelSecond(label);
            }
        }
        return AjaxResult.success("导入成功");
    }
}
